// pages/visitSign/visitSign.js
const app = getApp()
const api = app.globalData.api
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileList:[],
  },

  
  /**上传文件 */
  afterRead(event) {
    let that = this;
    const { file } = event.detail;

    api.uploadFileCRM(app.root+'?cla=fileUploadApp&fun=uploadFile',file,{power: 'visitPlan',fileType:'访客计划'}).then((res)=>{
      // 上传完成需要更新 fileList
           const { fileList = [] } = that.data;
           fileList.push({ ...file, url: res.result.src,id:res.result.id });
           that.setData({ fileList });
         })
  },
  /**删除文件 */
  delFile(event){
    let index = event.detail.index;
    let fileList = this.data.fileList;
    let newFileList=[];
    let id = '';
    fileList.forEach((v,i)=>{
      if(i==index){
        id=v.id;
      }else{
        newFileList.push(v);
      }
    })
  
    let that = this;
    api.curlPost('?cla=fileUploadApp&fun=delFile',{id:id,power:'visitPlan'}).then((res)=>{
      that.setData({
        fileList:newFileList
      })
    })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let id = options.id;
    that.requestData(id);
    return;
    wx.getLocation({
      type: 'gcj02',
      success (res) {
        api.curlPost('?cla=clientAppClass&fun=latLngToAddr',{lat:res.latitude,lng:res.longitude}).then((v)=>{
          that.setData({
            location: v.data.address,
            lat:res.latitude,
            lng:res.longitude
          })
        })
       
      }
     })
  },

  requestData(id){
    let that = this;
    api.curlPost('?cla=clientAppClass&fun=detailVisitPlan',{id:id}).then((res)=>{
      res.data.memberName = res.data.memberName.join(',');
      that.setData({
        info:res.data,
        id:res.data.id,
        // fileList:res.data.file
      })
    })
  },
  submit(){
    let para = {
      id:this.data.id,
      lat:this.data.lat,
      lng:this.data.lng
    }
    api.curlPost('?cla=clientAppClass&fun=visitPlanSign',para).then((res)=>{
      wx.navigateBack({
        delta:1
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})